use Test::More qw/no_plan/;
use Cwd;
use FindBin;
use lib Cwd::abs_path("$FindBin::Bin/../../../../lib");
use lib Cwd::abs_path("$FindBin::Bin/../../../../generate/MyProject/lib");
use strict;
use warnings;

use MyProject;
ok my $p = MyProject->Template->Parser->new;
$p->{pragma} = '';

{
    my($src, $st);
    ok $st = $p->compile(\($src = q{join("#", @{foo})}));
    is $st->{sub}, q{join("#", @{$_[0]->var('foo')})};
    
    ok $st = $p->compile(\($src = q{foo.bar.bar(1).baz(x || 100)}));
    is $st->{sub}, q{$_[0]->var('foo', ['.', 'bar'], ['.', 'bar', [1]], ['.', 'baz', sub{ [$_[0]->var('x') || 100] }])};
    
    ok $st = $p->compile(\($src = q{foo.bar['baz'][0][-1]["-${x}-"]}));
    is $st->{sub}, q%$_[0]->var('foo', ['.', 'bar'], ['[', 'baz'], ['[', 0], ['[', -1], ['[', sub{ "-".$_[0]->var('x')."-" }])%;
    
    ok $st = $p->compile(\($src = '"foo$a@a${bar.bar}baz"'));
    is $st->{sub}, q{"foo\$a\@a".$_[0]->var('bar', ['.', 'bar'])."baz"};
    
    ok $st = $p->compile(\($src = q{xxx =~ /^.+\z/}));
    is $st->{sub}, q{$_[0]->var('xxx') =~ /^.+\z/};
    
    ok $st = $p->compile(\($src = q{xxx =~ m{^\w+${foo}\w+\z}}));
    is $st->{sub}, q{$_[0]->var('xxx') =~ m{^\w+(??{ $_[0]->var('foo') })\w+\z}};
    
    ok $st = $p->compile(\($src = q{foo = bar}));
    is $st->{sub}, q{$_[0]->{my}{foo} = $_[0]->var('bar')};
    
    ok $st = $p->compile(\($src = q{foo = FOO = 1}));
    is $st->{sub}, q{$_[0]->{my}{foo} = $_[0]->{our}{FOO} = 1};
    
    ok $st = $p->compile(\($src = q{foo = {bar => baz, hoge, fuga}}));
    is $st->{sub}, q{$_[0]->{my}{foo} = {bar => $_[0]->var('baz'), $_[0]->var('hoge'), $_[0]->var('fuga')}};
    
    ok $st = $p->compile('/.+/');
    is $st->{sub}, '/.+/';
    
    ok $st = $p->compile(' /.+/ ');
    is $st->{sub}, ' /.+/ ';
    
    ok $st = $p->compile('xxx =~ s/././g');
    is $st->{sub}, q{$_[0]->{my}{xxx} =~ s/././g};
    
    ok $st = $p->compile('xxx =~ s{.}{.}');
    is $st->{sub}, q{$_[0]->{my}{xxx} =~ s{.}{.}};
    
    
    local $p->{ln} = 1;
    ok !$p->compile('');
    is $p->error, "Syntax error";
    
    ok !$p->compile(' ');
    is $p->error, "Syntax error near ` `";
    
    ok !$p->compile(q{join('#', 1, 2});
    is $p->error, "Missing right bracket ')' near `join('#', 1, 2`";
    
    ok !$p->compile('(1, 2, 3');
    is $p->error, "Missing right bracket ')' near `(1, 2, 3`";
    
    ok !$p->compile('x || [1 .. 10');
    is $p->error, "Missing right bracket ']' near `[1 .. 10`";
    
    ok !$p->compile('x + @{foo.bar');
    is $p->error, "Missing right bracket '}' near `\@{foo.bar`";
    
    ok !$p->compile('1, 2');
    is $p->error, "Syntax error near `1, `";
    
    ok !$p->compile('"x');
    is $p->error, qq{Can't find string terminator '"' anywhere near `"x`};
}
